import 'package:flutter/material.dart';
import 'package:flutter_navigator/Core/OSState.dart';
import 'package:flutter_navigator/OSNavigator/OSNavigator.dart';

import 'Model/VideoModel.dart';

/// 状态管理类 之 InheritedWidget
class AMCommonStateWidget extends InheritedWidget {
  final int count;
  AMCommonStateWidget({required this.count, required Widget child})
      : super(child: child);

  static AMCommonStateWidget? of(BuildContext context) {
    // 沿着Element树，去找最近的AMCommonStateElement, 从Element中取出Widget对象
    return context.dependOnInheritedWidgetOfExactType();
  }

  @override
  bool updateShouldNotify(covariant AMCommonStateWidget oldWidget) {
    // TODO: implement updateShouldNotify
    return oldWidget.count != count;
  }
}

class AMHomePage extends StatefulWidget {
  const AMHomePage({Key? key}) : super(key: key);
  @override
  _AMHomePageState createState() => _AMHomePageState();
}

class _AMHomePageState extends OSState<AMHomePage> {
  var listener;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    OSNavigator.getInstance().addListener(this.listener = (current, pre) {
      print("当前页面：${current.page}");
      print("上一个页面：${pre.page}");
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    OSNavigator.getInstance().removeListener(this.listener);
    super.dispose();
  }

  // 变量声明在 build方法外面，如果在build内部声明，重新build每次都会声明，相当于变量永远是新的，值永远是初始化的值
  var count = 15;
  Widget build(BuildContext context) {
    // 默认初始化10
    return Scaffold(
      appBar: AppBar(
        title: Text("逻辑资源"),
      ),
      body: AMCommonStateWidget(
        count: count,
        child: Column(
          children: [
            Container(
              height: 110,
              width: double.infinity,
              child: Card(
                color: Colors.orange,
                child: Column(
                  children: [
                    MaterialButton(
                      color: Colors.redAccent,
                      textColor: Colors.white,
                      onPressed: () {
                        OSNavigator.getInstance().onJumpTo(
                            RouteStatus.homeDetail,
                            args: {"videoModel": VideoModel(180)});
                      },
                      child: Text("点击跳转详情"),
                    ),
                    MaterialButton(
                      color: Colors.redAccent,
                      textColor: Colors.white,
                      onPressed: () {
                        // 修改变量
                        setState(() {
                          count++;
                        });
                      },
                      child: Text("计数器+1"),
                    ),
                  ],
                ),
              ),
            ),
            AMCardView01(),
            AMCardView02()
          ],
        ),
      ),
    );
  }
}

class AMCardView01 extends StatelessWidget {
  const AMCardView01({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var resCount = AMCommonStateWidget.of(context)?.count;
    resCount ??= 10;

    return Container(
      height: 80,
      width: double.infinity,
      child: Card(
        color: Colors.orange,
        child: Column(
          children: [
            Center(
              child: Text("当前的数量为$resCount"),
            )
          ],
        ),
      ),
    );
  }
}

class AMCardView02 extends StatelessWidget {
  const AMCardView02({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var resCount = AMCommonStateWidget.of(context)?.count;
    resCount ??= 10;

    return Container(
      height: 80,
      width: double.infinity,
      child: Card(
        color: Colors.yellow,
        child: Column(
          children: [
            Center(
              child: Text("当前的数量为$resCount"),
            )
          ],
        ),
      ),
    );
  }
}
